package algorithm;

public class T00063 {
    public static void main(String[] args) {
        int[][] obstacleGrid = {{0,0,0},{0,1,0},{0,0,0}};
        System.out.println(new Solution().uniquePathsWithObstacles(obstacleGrid));
    }

    static class Solution {
        public int uniquePathsWithObstacles(int[][] obstacleGrid) {
            int m = obstacleGrid.length;
            int n = obstacleGrid[0].length;
            int[][] dp = new int[m][n];
            dp[0][0] = 1;
            for (int i = 0; i < m; i ++) {
                for (int j = 0; j < n; j ++) {
                    if (i == 0 && j == 0) continue;
                    dp[i][j] = dist(obstacleGrid, dp, i - 1, j) + dist(obstacleGrid, dp, i, j - 1);
                }
            }
            return dp[m - 1][n - 1];
        }

        public int dist(int[][] obstacleGrid, int[][] dp, int i, int j) {
            if (i < 0 || j < 0 || obstacleGrid[i][j] == 1) return 0;
            return dp[i][j];
        }
    }
}
